package yyr.cn.com.javadesignmode;

import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

/**
 * Java  设计模式
 */
public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        RecyclerView rView = (RecyclerView) findViewById(R.id.rv_recyclerview);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(MainActivity.this);
        rView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
        rView.setLayoutManager(linearLayoutManager);

        String[] names = getResources().getStringArray(R.array.name_list);
        RVAdapter rvAdapter = new RVAdapter();
        rvAdapter.setViewOnClick(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Uri uri=Uri.parse("yyr://test");
                Intent intent=new Intent(Intent.ACTION_VIEW,uri);
                startActivity(intent);
            }
        });
        rvAdapter.setDatas(names);
        rView.setAdapter(rvAdapter);
    }

    public class RVAdapter extends RecyclerView.Adapter<RVAdapter.AdapterHolder> {


        public View.OnClickListener clickListener;
        String[] names;

        public void setDatas(String[] names) {
            this.names = names;
        }

        public void setViewOnClick(View.OnClickListener click) {
            this.clickListener = click;
        }

        @Override
        public RVAdapter.AdapterHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            /**
             * inflater在inflate一个xml时，需要知道parent的类型，才能生成对应的LayoutParams，才可以把xml根节点的attrs（如layout_width）读进去，
             * 所以当   第二个参数为 null 的时候  怎么设置 item  布局的 宽   都是不起作用的
             */
            View view = LayoutInflater.from(MainActivity.this).inflate(R.layout.layout_item, parent, false);
            AdapterHolder adapterHolder = new AdapterHolder(view);
            return adapterHolder;
        }

        @Override
        public void onBindViewHolder(RVAdapter.AdapterHolder holder, int position) {
            holder.name.setText(names[position]);

        }

        @Override
        public int getItemCount() {
            return names.length;
        }

        class AdapterHolder extends RecyclerView.ViewHolder {
            TextView name;

            public AdapterHolder(View itemView) {
                super(itemView);
                name = itemView.findViewById(R.id.name);
                itemView.setOnClickListener(clickListener);
            }
        }
    }
}
